package com.exam.scjp5.objectives.section_1;

public class ExampleStaticMethod
{
    public static void main(String args[])
    {
        // Static methods will be called depending on the declared reference type.
        // Here, the declared type of 'p' is StaticMethodParent. 
        // But its reference is StaticMethodChild.
        // But system will call StaticMethodParent's static method.
        StaticMethodParent p = new StaticMethodChild();
        p.staticMethod(null);
        
        StaticMethodChild c = new StaticMethodChild();
        c.staticMethod(null);
        
        
        // See the following. ps[0]'s reference is StaticMethodChild but when invoke,
        // it will call StaticMethodParent's staticMethod()
        StaticMethodParent[] ps = { new StaticMethodChild(), new StaticMethodParent() };
        for(int i=0; i < ps.length; i++)
        {
            ps[i].staticMethod(null);
        }
        
        // When you call StaticMethodChild2 's static method,
        StaticMethodChild2 c2 = new StaticMethodChild2();
        c2.staticMethod(null); // It will print 'PARENT:staticMethod()'
        StaticMethodChild2.staticMethod(null); // It will print 'PARENT:staticMethod()'        
    }
}

/**
 * This parent class has a static method.
 */
class StaticMethodParent
{
    public static int i = 0;
    
    public static void staticMethod(ParentDefaultConstructor p)
    {
        System.out.println("PARENT:staticMethod()");
    }
}

/**
 * This child class will inherit the parent class StaticMethodParent
 */
class StaticMethodChild extends StaticMethodParent
{
    // Child will inherit parent's static method. But it can't be overridden.
    // You may think the following method overrides the parent's method.
    // Totally wrong !!! This is just redefining the parent's static method.
    // If you do not redefine, whenever you invoke StaticMethodChild's static method,
    // then system will run only parent's method. See StaticMethodChild2
    public static void staticMethod(ParentDefaultConstructor c)
    {
        System.out.println("CHILD:staticMethod()");
    }
}


/**
 * This child class will inherit the parent class StaticMethodParent 
 * but did not redefine static method
 */
class StaticMethodChild2 extends StaticMethodParent
{
    // Child will inherit parent's static method. 
    // But it does not redefine static method.
}